﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Default3 : System.Web.UI.Page
{
    string strCon = System.Configuration.ConfigurationManager.ConnectionStrings["xxnewsConnectionString"].ConnectionString;
    SqlConnection sqlcon;
    SqlCommand sqlcom;
    string jinggao4;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["pn"] == "" || Session["pn"] == null)
        {
            Response.Redirect("default.aspx");
        }
        if (Session["pngeren"] == "" || Session["pngeren"] == null)
        {
            Response.Redirect("default.aspx");
        }
        if (!IsPostBack)
        {
            Label_nian.Text = DateTime.Now.Year.ToString();
            Label_yue.Text = DateTime.Now.Month.ToString();
            Label1.Text = Session["pnchejian"].ToString();
            Label2.Text = Session["pn"].ToString();
            Label3.Text = Session["pngeren"].ToString();
            if (GridView1.Rows.Count > 0)
            {
                DropDownList_lei.Items.Add(new ListItem("轨道车"));
                //Label_gonglibiao.Visible = false;
                //Label_yunxing.Visible = false;
                //TextBox_gonglibiao.Visible = false;
                //TextBox_yunxing.Visible = false;
            }
            if (GridView2qiche.Rows.Count > 0)
            {
                DropDownList_lei.Items.Add(new ListItem("汽车"));
                //Label_gonglibiao.Visible = true;
                //Label_yunxing.Visible = true;
                //TextBox_gonglibiao.Visible = true;
                //TextBox_yunxing.Visible = true;
            }
            if ( GridView3fadianji.Rows.Count > 0)
            {
                DropDownList_lei.Items.Add(new ListItem("发电机"));
                //Label_gonglibiao.Visible = true;
                //Label_yunxing.Visible = true;
                //TextBox_gonglibiao.Visible = true;
                //TextBox_yunxing.Visible = true;
            }
        }
    }
    protected void DropDownList_lei_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList_lei.SelectedValue == "请选择")
        {
            DropDownList_chehao.Items.Clear();
            DropDownList_chehao.Items.Add(new ListItem("请选择"));
            SqlDataSource_chehao.SelectCommand = "";
            DropDownList_mingcheng.Items.Clear();
            DropDownList_mingcheng.Items.Add(new ListItem("请选择"));
            Label_danjia.Text = "/";
        }
        else if (DropDownList_lei.SelectedValue == "汽车")
        {
            DropDownList_chehao.Items.Clear();
            DropDownList_chehao.Items.Add(new ListItem("请选择"));
            SqlDataSource_chehao.SelectCommand = "SELECT chehao FROM tcjcb_qiche WHERE banzu1='" + Label2.Text + "' ORDER BY [xuhao]";
            DropDownList_mingcheng.Items.Clear();
            DropDownList_mingcheng.Items.Add(new ListItem("请选择"));
            Label_gonglibiao.Visible = true;
            Label_yunxing.Visible = true;
            TextBox_gonglibiao.Visible = true;
            TextBox_yunxing.Visible = true;
            Label_danjia.Text = "/";
        }
        else if (DropDownList_lei.SelectedValue == "轨道车")
        {
            DropDownList_chehao.Items.Clear();
            DropDownList_chehao.Items.Add(new ListItem("请选择"));
            SqlDataSource_chehao.SelectCommand = "SELECT chehao FROM tcjcb WHERE banzu1='" + Label2.Text + "' ORDER BY [xuhao]";
            DropDownList_mingcheng.Items.Clear();
            DropDownList_mingcheng.Items.Add(new ListItem("请选择"));
            Label_gonglibiao.Visible = false;
            Label_yunxing.Visible = false;
            TextBox_gonglibiao.Visible = false;
            TextBox_yunxing.Visible = false;
            Label_danjia.Text = "/";
        }
        else if (DropDownList_lei.SelectedValue == "发电机")
        {
            DropDownList_chehao.Items.Clear();
            DropDownList_chehao.Items.Add(new ListItem("请选择"));
            SqlDataSource_chehao.SelectCommand = "SELECT chehao FROM tcjcb_fadianji WHERE banzu1='" + Label2.Text + "' ORDER BY [xuhao]";
            DropDownList_mingcheng.Items.Clear();
            DropDownList_mingcheng.Items.Add(new ListItem("请选择"));
            Label_gonglibiao.Visible = false;
            Label_yunxing.Visible = false;
            TextBox_gonglibiao.Visible = false;
            TextBox_yunxing.Visible = false;
            Label_danjia.Text = "/";
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (DropDownList_lei.SelectedValue == "请选择")
        {
            ClientScript.RegisterStartupScript(this.GetType(), "aa1", "<SCRIPT language=javascript>alert('请选择类别！');</SCRIPT>");
            DropDownList_lei.Focus();
            return;
        }
        else if (DropDownList_lei.SelectedValue == "汽车")
        {
            if (TextBox_gonglibiao.Text == "")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "aa1", "<SCRIPT language=javascript>alert('请输入本次公里表底数！');</SCRIPT>");
                TextBox_gonglibiao.Focus();
                return;
            }
            if (TextBox_yunxing.Text == "")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "aa1", "<SCRIPT language=javascript>alert('请输入运行公里数！');</SCRIPT>");
                TextBox_yunxing.Focus();
                return;
            }
        }
        if (DropDownList_chehao.SelectedValue == "请选择")
        {
            ClientScript.RegisterStartupScript(this.GetType(), "aa1", "<SCRIPT language=javascript>alert('请选择车号！');</SCRIPT>");
            DropDownList_chehao.Focus();
            return;
        }
        if (DropDownList_mingcheng.SelectedValue == "请选择")
        {
            ClientScript.RegisterStartupScript(this.GetType(), "aa1", "<SCRIPT language=javascript>alert('请选择燃油种类！');</SCRIPT>");
            DropDownList_mingcheng.Focus();
            return;
        }
        if (TextBox_banzushuliang.Text == "")
        {
            ClientScript.RegisterStartupScript(this.GetType(), "aa1", "<SCRIPT language=javascript>alert('请输入燃油数量！');</SCRIPT>");
            TextBox_banzushuliang.Focus();
            return;
        }
        else if (TextBox_banzushuliang.Text == "0")
        {
            ClientScript.RegisterStartupScript(this.GetType(), "aa1", "<SCRIPT language=javascript>alert('输入的燃油数量不能为0！');</SCRIPT>");
            TextBox_banzushuliang.Focus();
            return;
        }
        else
        {
            for (int j = 0; j < TextBox_banzushuliang.Text.Trim().Length; j++)
            {
                char a;
                int k;
                a = Convert.ToChar(TextBox_banzushuliang.Text.Substring(j, 1));
                k = (int)a;
                if (k < 48 || k > 57)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "aa1", "<SCRIPT language=javascript>alert('请在燃油数量中输入整数！');</SCRIPT>");
                    TextBox_banzushuliang.Focus();

                    return;
                }
            }
        }
        try
        {
            string sqlstr2 = "INSERT INTO ranyouxiaohao ([lei],[chejian],[banzu],[chehao],[nian],[yue],[mingcheng],[danjia],[gonglibiao],[yunxing],[banzushuliang],[addname],[addtime],[addip],[zt])VALUES(";
            sqlstr2 += "'" + DropDownList_lei.SelectedValue + "',";
            sqlstr2 += "'" + Label1.Text + "',";
            sqlstr2 += "'" + Label2.Text + "',";
            sqlstr2 += "'" + DropDownList_chehao.SelectedValue + "',";
            sqlstr2 += "'" + Label_nian.Text + "',";
            sqlstr2 += "'" + Label_yue.Text + "',";
            sqlstr2 += "'" + DropDownList_mingcheng.SelectedValue + "',";
            sqlstr2 += "'" + Label_danjia.Text + "',";
            sqlstr2 += "'" + TextBox_gonglibiao.Text + "',";
            sqlstr2 += "'" + TextBox_yunxing.Text + "',";
            sqlstr2 += TextBox_banzushuliang.Text + ",";
            sqlstr2 += "'" + Label3.Text + "',";
            sqlstr2 += "'" + DateTime.Now.ToString() + "',";
            sqlstr2 += "'" + Page.Request.UserHostAddress.ToString().Trim() + "',";
            sqlstr2 += "1)";


            //Response.Write(sqlstr2);
            //Response.End();
            sqlcon = new SqlConnection(strCon);
            sqlcom = new SqlCommand(sqlstr2, sqlcon);
            sqlcon.Open();
            sqlcom.ExecuteNonQuery();
            sqlcon.Close();
            string jinggao3 = "<SCRIPT language=javascript>alert('";
            jinggao3 += "燃油计划提报成功！";
            jinggao3 += "');</SCRIPT>";
            ClientScript.RegisterStartupScript(this.GetType(), "aa3", jinggao3);
            Button1.Visible = false;
            Button2.Visible = true;
            Button3.Visible = true;
            //ClientScript.RegisterStartupScript(this.GetType(), "aa4", "<SCRIPT language=javascript>document.location.href='index.aspx';</SCRIPT>");
        }
        catch (SqlException)
        {
            throw;
        }


    }

    protected void DropDownList_mingcheng_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridView3ranyou.DataBind();
        if (GridView3ranyou.Rows.Count > 0)
        {
            Label_danjia.Text = GridView3ranyou.Rows[0].Cells[0].Text;
        }
        else
        {
            Label_danjia.Text = "/";
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        DropDownList_lei.SelectedValue = "请选择";
        DropDownList_chehao.Items.Clear();
        DropDownList_chehao.Items.Add(new ListItem("请选择"));
        DropDownList_mingcheng.Items.Clear();
        DropDownList_mingcheng.Items.Add(new ListItem("请选择"));
        Button1.Visible = true;
        Button2.Visible = false;
        Button3.Visible = false;
        Label_danjia.Text = "/";
        TextBox_banzushuliang.Text = "";
        TextBox_gonglibiao.Text = "";
        TextBox_yunxing.Text = "";
    }
}